#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define LISTEN_PORT 8888
#define BUFFER_SIZE 1024

int main() {
    int ret = -1;
    int client_fd = -1;
    char buff[BUFFER_SIZE] = "Hello Server, I'm Client.";
    struct sockaddr_in server_addr = {0};
    struct sockaddr_in client_addr = {0};
    struct sockaddr *ptr_server_addr = (struct sockaddr *)&server_addr;

    client_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (client_fd == -1) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    memset(ptr_server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(LISTEN_PORT);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    ret = connect(client_fd, ptr_server_addr, sizeof(server_addr));
    if (ret == -1) {
        perror("connect");
        exit(EXIT_FAILURE);
    }

    printf("Connected to server.\n");
    write(client_fd, buff, BUFFER_SIZE);
    memset(buff, 0, BUFFER_SIZE);
    read(client_fd, buff, BUFFER_SIZE);
    printf("Server said: %s\n", buff);

    close(client_fd);
    return 0;
}
